package com.repast.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.repast.common.ServerResponse;
import com.repast.dao.RestaurantInfoMapper;
import com.repast.pojo.RestaurantInfo;
import com.repast.service.IRestaurantInfoService;
import com.repast.vo.RsTimeInfoVo;

@Service
public class RestaurantInfoServiceImpl implements IRestaurantInfoService {
	@Autowired
	private RestaurantInfoMapper rsMapper;

	public ServerResponse selectRsTime() {
		List<RestaurantInfo> list = rsMapper.selectRestaurant();
		if (list.size() != 0) {
			for (RestaurantInfo restaurantInfo : list) {
				if (restaurantInfo != null) {
					if (restaurantInfo.getRestaurantInfoAppointment() == 0) {
						// 如果时间状态为0,表示,预约和外卖时间和营业时间是一致的
						String restaurantInfoHours = restaurantInfo.getRestaurantInfoHours();
						String[] time = restaurantInfoHours.split("~");
						return ServerResponse.createBySuccess(time);
					}
					if (restaurantInfo.getRestaurantInfoAppointment() != 0) {
						String[] time = restaurantInfo.getRestaurantInfoAppointmentHours().split("~");
						return ServerResponse.createBySuccess(time);
					}
				}
			}
		}
		return ServerResponse.createByErrorMessage("系统异常,没有查到店面相关信息");
	}

	public ServerResponse judgeBusness() {
		List<RestaurantInfo> list = rsMapper.selectRestaurant();
		if (list.size() != 0) {
			for (RestaurantInfo restaurantInfo : list) {
				String day = restaurantInfo.getRestaurantInfoDay();
				String[] days = day.split(",");

				List<String> weekList = new ArrayList();
				String str[] = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };// 字符串数组
				for (String string : days) {
					weekList.add(str[(Integer.parseInt(string) - 1)]);
				}
				Calendar rightNow = Calendar.getInstance();
				int week = rightNow.get(rightNow.DAY_OF_WEEK);
				System.out.println(str[week-2]);
				for (String string : weekList) {
					if(string==str[week-2]){
						
						if (restaurantInfo.getRestaurantInfoAppointment() == 0) {
							RsTimeInfoVo rsTimeInfo = new RsTimeInfoVo();
							// 如果时间状态为0,表示,预约和外卖时间和营业时间是一致的
							String restaurantInfoHours = restaurantInfo.getRestaurantInfoHours();
							String[] time = restaurantInfoHours.split("~");
							rsTimeInfo.setBusnessTime(time);
							rsTimeInfo.setTakeOutTime(time);
							return ServerResponse.createBySuccess(rsTimeInfo);
						}
						if (restaurantInfo.getRestaurantInfoAppointment() != 0) {
							RsTimeInfoVo rsTimeInfo = new RsTimeInfoVo();
							String restaurantInfoHours = restaurantInfo.getRestaurantInfoHours();
							String[] time = restaurantInfoHours.split("~");
							rsTimeInfo.setBusnessTime(time);
							String[] takeOutTime = restaurantInfo.getRestaurantInfoAppointmentHours().split("~");
							rsTimeInfo.setTakeOutTime(takeOutTime);
							return ServerResponse.createBySuccess(rsTimeInfo);
						}
						
					}
				}
			}
		}
		return ServerResponse.createByErrorMessage("对不起今天打烊了");
	}

}
